package cj.web.login.controllers;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.dao.ConfigInfoDAO;
import cj.web.admin.utils.DateUtils;
import cj.web.admin.utils.RsaUtil;

@Path("/sk")
public class SecurityKeyController extends BaseController {
	
	private static Logger logger = Logger.getLogger(SecurityKeyController.class.getName());
	
	@Autowired
	private ConfigInfoDAO configInfoDao;
	
	@Get("show")
	public String show(Model model){
		return "/views/key/key.vm";
	}

	@Post("saveKey")
	public String saveKey(Model model,@Param("key") String key){
		if(key==null || "".equals(key)){
			return "r:../logout";
		}
		
		String l_key = configInfoDao.querySecurityKey();
		
		if(l_key!=null && !"".equals(l_key)){
			//存在Key
			configInfoDao.updateSecurityKey(key);
		}else{
			//不存在Key
			model.add("msg","不存在Key,验证失败!");
			return "/views/key/key.vm";
		}
		
		return "r:../logout";
	}
	
	@Get("delayKey")
	public String delayKey(Model model) throws Exception{
		String l_key = configInfoDao.querySecurityKey();
		
		//服务器 当前时间
		String content = DateUtils.getMinusTime(DateUtils.DATE_SMALL_STR, -30);
		String encrypt = RsaUtil.encryptByPublicKey(content);
		
		if(l_key!=null && !"".equals(l_key)){
			//存在Key
			configInfoDao.updateSecurityKey(encrypt);
		}else{
			//不存在Key
			model.add("msg","不存在Key,验证失败!");
			return "/views/key/key.vm";
		}
		
		return "r:../logout";
	}
}